Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Content-Length set on requests with known length bodies #981

Merged
merged 1 commit into from
Aug 9, 2023

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Aug 7, 2023

Hey! 👋 This PR ensures requests with known length bodies from service bindings, or other internal requests from bindings (e.g. R2Bucket#put()), share that known length with the handler.

I'm not sure whether this change needs a compatibility flag. I'm leaning towards no, as we're only adding a header if Content-Length and Transfer-Encoding are missing. Although as highlighted by the new test, if a FixedLengthStream's readable was used as a body, previously Content-Length wouldn't have been set. This is a user facing change, but I don't think it's breaking.

@mrbbot mrbbot requested a review from kentonv August 7, 2023 11:28
@mrbbot mrbbot force-pushed the bcoll/w2w-known-content-length branch 3 times, most recently from c68ab49 to 0cc7836 Compare August 7, 2023 12:08
if (method != kj::HttpMethod::HEAD && method != kj::HttpMethod::GET &&
headers.get(kj::HttpHeaderId::CONTENT_LENGTH) == nullptr &&
headers.get(kj::HttpHeaderId::TRANSFER_ENCODING) == nullptr) {
KJ_IF_MAYBE(l, requestBody.tryGetLength()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the length isn't known, should we set Transfer-Encoding: chunked to emulate what an actual HTTP connection would do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, sure! 🙂

// If the request doesn't specify "Content-Length" or "Transfer-Encoding", set "Content-Length"
// to the body length if it's known. This ensures handlers for worker-to-worker requests can
// access known body lengths if they're set, without buffering bodies.
if (method != kj::HttpMethod::HEAD && method != kj::HttpMethod::GET &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GET can have a body. Instead of checking the method, maybe what you want is body != nullptr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GET can have a body?! Fun! Updated 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... it's quite disturbing, but some people use it and we had to add support for it in the early days of Workers.

@mrbbot mrbbot force-pushed the bcoll/w2w-known-content-length branch from 0cc7836 to 4ace1e4 Compare August 7, 2023 20:54
This ensures requests with known length bodies from service bindings,
or other internal requests from bindings (e.g. `R2Bucket#put()`),
share that known length with the handler.
@mrbbot mrbbot force-pushed the bcoll/w2w-known-content-length branch from 4ace1e4 to 6d25855 Compare August 7, 2023 20:57
@mrbbot mrbbot requested a review from kentonv August 7, 2023 21:07
@mrbbot mrbbot merged commit 8bb6c48 into main Aug 9, 2023
7 checks passed
@mrbbot mrbbot deleted the bcoll/w2w-known-content-length branch August 9, 2023 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants